package leetcode;

import java.util.LinkedList;

public class LC200 {
    int[] dx = {0,0,-1,1};
    int[] dy = {-1,1,0,0};
    int n,m;

    public void dfs(char[][] grid,boolean[][] visited, int x, int y) {
        visited[x][y] = true;
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny] == '1' && !visited[nx][ny]) {
                dfs(grid,visited,nx,ny);
            }
        }
    }
    //dfs
    public int numIslands(char[][] grid) {

        int m = grid.length;
        int n = grid[0].length;
        this.m = m;
        this.n = n;
        boolean[][] visited = new boolean[m][n];
        int cnt = 0;

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (!visited[i][j] && grid[i][j] == '1') {
                    cnt ++;
                    dfs(grid,visited,i,j);
                }
            }
        }
        return cnt;
    }






}
